/*
 * Copyright 2010 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma.client.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.shalma.client.ui.icon.ShalmaResources;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * <h3>CSS Style Rules</h3>
 * <ul class='css'>
 * <li>.optersoft-FileUpload</li>
 * </ul>
 */
public class FileUpload extends Composite implements ClickListener, FormHandler {

	public static final int UPLOADING = 1;
	public static final int UPLOADED = 2;
	public static final int FAILED = 3;
	public static final int CLOSE = 4;

	public static final String ACTION = "shalma.presentation.FileUpload";
	public static final String FILE = "file";
	public static final String UPLOADED_MESSAGE = "UPLOADED";

	private FormPanel form;
	private Hidden fileName;

	private String name;

	private Image no;
	private Image yes;

	private List listeners;

	// ///

	public FileUpload() {
		super();

		form = new FormPanel();
		form.setAction(ACTION);
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.addFormHandler(this);

		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(4);
		form.setWidget(panel);

		fileName = new Hidden(FILE);
		panel.add(fileName);

		com.google.gwt.user.client.ui.FileUpload upload = new com.google.gwt.user.client.ui.FileUpload();
		upload.setName("upload");
		panel.add(upload);

		FlowPanel buttons = new FlowPanel();
		no = new Image(ShalmaResources.instance.no());
		no.addClickListener(this);
		buttons.add(no);
		yes = new Image(ShalmaResources.instance.yes());
		yes.addClickListener(this);
		buttons.add(yes);
		panel.add(buttons);

		initWidget(form);

		setStyleName("shalma-FileUpload");
	}

	// ///

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void addFileUploadListener(FileUploadListener listener) {
		if (listeners == null)
			listeners = new ArrayList();
		listeners.add(listener);
	}

	public void removeFileUploadListener(FileUploadListener listener) {
		if (listeners != null)
			listeners.remove(listener);
	}

	// ///

	public void onClick(Widget sender) {

		if (sender == no) {
			fireEvent(CLOSE);
		} else if (sender == yes) {
			updateExtension();
			fileName.setValue(name);
			form.setVisible(false);
			form.submit();
		}
	}

	public void onSubmit(FormSubmitEvent event) {
		form.setVisible(true);
		fireEvent(UPLOADING);
	}

	public void onSubmitComplete(FormSubmitCompleteEvent event) {

		String result = event.getResults().toString();

		if (result != null
				&& result.equals("<pre>" + UPLOADED_MESSAGE + "</pre>"))
			fireEvent(UPLOADED);
		else
			fireEvent(FAILED);
	}

	// ///

	void updateExtension() {
		name = name.substring(0, name.lastIndexOf('.')) + ".jpg";
	}

	// ///

	private void fireEvent(int type) {

		if (listeners == null)
			return;

		for (Iterator it = listeners.iterator(); it.hasNext();)
			((FileUploadListener) it.next()).onEvent(this, type);
	}
}
